home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / createAndAssignShader.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  4.6 KB  |  156 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  Oct 6, 2000
  22. //  Author:         cg prod spec
  23. //
  24. //  Description:
  25. //      This script creates menu items to add shaders to an object.
  26. //
  27.  
  28. global proc showShadingGroupAE(string $shader)
  29. //
  30. //    If the option to show the attribute editor is on, then
  31. //    display the attribute editor for the object $shader.
  32. //
  33. {
  34.     if (`optionVar -q AEpopupWhenCreatingShaders`) {
  35.         string $cmd = ("showEditor " + $shader);
  36.         evalDeferred -lowestPriority $cmd;
  37.     }
  38. }
  39.  
  40. global proc createAndAssignShader (string $type, string $item)
  41. //
  42. //    Create a shader of the given type and assign it to the
  43. //    given object.
  44. //
  45. //    $type = type of shader
  46. //    $item = object to act on, if empty then use the selection list
  47. //
  48. {
  49.     string $objs[];
  50.     string $assignString = $item;
  51.     if ($item == "") {
  52.         // No object was specified in the call to this procedure, so we will
  53.         // assign the shader to whatever is on the current selection list 
  54.         // instead.
  55.         //
  56.         $objs = `ls -selection`;
  57.         $assignString = "the selected objects";
  58.     }
  59.     else
  60.     {
  61.         //
  62.         // The $item is always an object, never a component (ie face).
  63.         // If the current selection contains faces of the specified item, then
  64.         // we would rather assign the new shader to the specifically selected
  65.         // faces rather than the object as a whole. In particular, this allows
  66.         // users to select faces of a poly object and use the RMB menu to
  67.         // assign shaders to them.
  68.         // 
  69.  
  70.         string $selection[] = `ls -selection`;
  71.         int $i;
  72.  
  73.         for ($i = 0; $i < size($selection); $i++)
  74.         {
  75.             if (`match ($item + "\\.f\\[.*\\]") $selection[$i]` != "")
  76.             {
  77.                 // One part of the currently selection is faces of the
  78.                 // specified item. We will add the selected faces to the list
  79.                 // of objects to which the shader will be assigned.
  80.                 //
  81.                 $objs[size($objs)] = $selection[$i];
  82.                 $assignString = ("the selected faces of " + $item);
  83.             }
  84.         }
  85.  
  86.         if (size($objs) == 0)
  87.         {
  88.             // Try again with the shape. Face selection will be names
  89.             // after the shape when other shapes are parented below the
  90.             // transform.
  91.             string $shapes[] = `listRelatives -s $item`;
  92.             if (size($shapes) > 0 )
  93.             {
  94.                 string $shape = $shapes[0];
  95.                 for ($i = 0; $i < size($selection); $i++)
  96.                 {
  97.                     if (`match ($shape + "\\.f\\[.*\\]") $selection[$i]` != "")
  98.                     {
  99.                         $objs[size($objs)] = $selection[$i];
  100.                         $assignString = ("the selected faces of " + $shape);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.  
  106.         if (size($objs) == 0)
  107.         {
  108.             // There were no faces of the specified item in the current
  109.             // selection. Therefore, we will assign the shader to the entire
  110.             // object specified.
  111.             //
  112.             $objs[0] = $item;
  113.         }
  114.     }
  115.  
  116.     $material = `shadingNode -asShader $type`;
  117.     $sg = `sets -renderable true -noSurfaceShader true 
  118.                 -empty -name ($material + "SG")`;
  119.  
  120.     // Connect the material to the shading group
  121.     //
  122.     defaultNavigation 
  123.         -connectToExisting 
  124.         -source $material
  125.         -destination $sg;
  126.  
  127.     // Select the items to which the shading group is to be assigned.
  128.     //
  129.     select -r $objs;
  130.  
  131.     // Assign the shading group to the selected objects.
  132.     //
  133.     if( $type == "oceanShader" ){
  134.         connectAttr -f ($material + ".displacement") ($sg + ".displacementShader");
  135.         connectAttr -f "time1.outTime" ($material + ".time");
  136.         assignOceanShader $sg;
  137.     } else {
  138.         hyperShade -assign $sg;
  139.     }
  140.     if( $type == "fluidShape" ){
  141.         connectAttr -f "time1.outTime" ($material + ".currentTime");
  142.     }
  143.  
  144.     print ("// Result: Created " + $type + " shader and assigned to "
  145.         + $assignString + ". //\n") ;
  146.  
  147.     showShadingGroupAE($sg);
  148.     
  149. //    string $state = `menuItem -q -cb attrEdPopupItem`;
  150. //    if ($state == 1)
  151. //    {
  152. //        string $cmd = ("showEditor " + $sg);
  153. //        evalEcho $cmd;
  154. //    }
  155. }
  156.